/* s^2 - lex HTTP/1 requests
   Copyright (C) 2018 Ariadne Devos

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#ifndef _sHT_WEB_LEX1_H
#define _sHT_WEB_LEX1_H

#include <stdint.h>
#include <sHT/bitvec.h>
#include <sHT/test.h>

extern const uint8_t s2_http_token[32];

/** Is @var{c} a token character, according to HTTP/1?

  From (RFC 2616, 2.2 Basic Rules, p. 16-17):
  CHAR = <any US-ASCII character (octets 0 - 127)>
  CTL = <any US-ASCII control character (octets 0 - 31) and DEL (127)>
  token = 1*<any CHAR except CTLs or seperators>
  seperators = "(" | ")" | "<" | ">" | "@" | "," | ";" | ":" | "\" |
    <"> | "/" | "[" | "]" | "?" | "=" | "{" | "}" | SP | HT

  The test may speculatively be incorrect.
  This must be called in control position. */
__attribute__((const))
__attribute__((always_inline))
static inline _Bool
s2_http_token_p(uint8_t c)
{
	return sHT_bit_test(s2_http_token, c);
}

/** Is @var{c} a space character, according to HTTP/1?

  From (RFC 2616, 2.2 Basic Rules, p. 16):
  SP = <US-ASCII SP, space (32)>.

  The test may speculatively be incorrect.
  This must be called in control position. */
__attribute__((const))
__attribute__((always_inline))
static inline _Bool
s2_http_space_p(uint8_t c)
{
	return sHT_eq(c, 32);
}

/* TODO: accumulate bytes in a buffer,
  until it would overflow or a byte doesn't belong to a certain character class.
  (Then, do a table lookup or something else.) */

#endif
